package LeetCode;

//
// Created by ✎﹏ℳ๓独赏☆独醉 on 2024/4/25.
// 
public class 合并两个有序链表 {
    public static class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }
    public static void main(String[] args) {

    }
    public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
        ListNode newList = new ListNode(0,null);
        ListNode temp = newList;
        while(list1 != null && list2 != null){
            if(list1.val < list2.val){
                temp.next = list1;
                list1 = list1.next;
            }else {
                temp .next = list2;
                list2 = list2.next;
            }
            temp = temp.next;
        }
        while (list1 != null){
            temp.next = list1;
            list1 = list1.next;
            temp = temp.next;
        }
        while (list2 != null){
            temp.next = list2;
            list2 = list2.next;
            temp = temp.next;
        }
        return newList.next;
    }
}
